package com.thinkit.bigdata.web.service.impl.sec;

import com.thinkit.bigdata.core.feature.orm.mybatis.Page;
import com.thinkit.bigdata.web.dao.sec.SysRoleMapper;
import com.thinkit.bigdata.web.model.sec.SysRole;
import com.thinkit.bigdata.web.model.sec.SysRoleExample;
import com.thinkit.bigdata.web.service.sec.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Created by lihongli on 2018/9/7
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private SysRoleMapper mapper;

    @Override
    public List<SysRole> selectByExample(SysRoleExample example) {
        return mapper.selectByExample(example);
    }

    @Override
    public List<SysRole> selectByExample(Page<SysRole> page, SysRoleExample example) {
        return mapper.selectByExample(page, example);
    }

    @Override
    public Integer countRole(SysRoleExample example) {
        return mapper.countByExample(example);
    }

    @Override
    public Integer insert(SysRole record) {
        return mapper.insertSelective(record);
    }

    @Override
    public SysRole selectRoleByPK(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer update(SysRole record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Integer updateStatusBatch(SysRole record, SysRoleExample example) {
        return mapper.updateByExampleSelective(record, example);
    }

    @Override
    public Integer deleteRoleBatch(List<Long> idList) {
        // 获取所有子角色
        List<Long> list=selectSubRoleId(idList);
        int rowNum = 0;
        // 循环遍历删除所有角色
        for (Long roleId : list) {
            rowNum += mapper.deleteByPrimaryKey(roleId);
            // 删除用户与角色质检的关联关系
            mapper.deleteUserRoleRel(roleId);
            // 删除菜单与角色之间的关联关系
            mapper.deleteResourceRoleRel(roleId);
        }
        return rowNum;
    }

    // 校验角色是否在被使用
    public boolean checkRoleIsUsing(List<Long> idList){
        List<Long> list=selectSubRoleId(idList);
        for (Long roleId : list) {
            if(mapper.checkRoleIsUsing(roleId) > 0)
                return true;
        }
        return false;
    }
    // 校验角色包含的子角色是否使用中
    public Integer checkRoleHavChild(List<Long> idList){
        List<Long> list=selectSubRoleId(idList);
        //包含子角色时查询子角色是否使用中
        if ( list.size()>idList.size()) {
            HashSet h1 = new HashSet(list);
            HashSet h2 = new HashSet(idList);
            h1.removeAll(h2);
            list.clear();
            //获取子角色集合
            list.addAll(h1);
            for (Long roleId : list) {
                if(mapper.checkRoleIsUsing(roleId) > 0){
                    //子角色使用中，禁止删除
                    return 0;
                }
            }
            //子角色未使用，提示是否一并删除
            return 1;
        }
        //无子角色，无需提示进入下一步
        return 2;
    }

    // 查询子角色信息 并将子角色信息也存入集合中
    private List<Long> selectSubRoleId(List<Long> idList){
        List<Long> nidList=new ArrayList<Long>();
        for (Long id : idList) {
            // 获取所有子角色
            SysRoleExample example = new SysRoleExample();
            example.createCriteria().andParentroleidEqualTo(id);
            List<SysRole> roleList = mapper.selectByExample(example);
            nidList.add(id);
            for (SysRole sysRole : roleList) {
                nidList.add(sysRole.getId());
            }
        }
        return nidList;
    }

    @Override
    public List<SysRole> selectRoleByUserId(Long userId) {
        return mapper.selectRoleByUserId(userId);
    }

    @Override
    public boolean RoleEquals(List<SysRole>  sysRole,SysRole role) {
            for (SysRole obj:sysRole){
                if (obj.getId().equals(role.getId())){
                    return true;
                }
            }
        return false;
    }
}
